class BatteryEnergyStorageSystem:
    def __init__(self, Eb_cap, Td, SOCl, SOCu):
        """
        初始化 BESS 参数
        :param Eb_cap: BESS 能量容量 (Wh)
        :param Td: 调度间隔时间 (min)
        :param SOCl: BESS 的 SOC 下限
        :param SOCu: BESS 的 SOC 上限
        """
        self.Eb_cap = Eb_cap  # BESS 能量容量 (Wh)
        self.Td = Td         # 调度间隔时间 (min)
        self.SOCl = SOCl     # BESS 的 SOC 下限
        self.SOCu = SOCu     # BESS 的 SOC 上限
        self.SOC = 0.5       # 初始 SOC
        self.previous_status = None  # 上一个调度间隔的状态 (charge/discharge)

    def online_power_dispatching(self, P_fl_current, P_fu_current, P_fl_next, P_fu_next):
        """
        在线功率调度
        :param P_fl_current: 当前调度间隔的下边界预测功率 (W)
        :param P_fu_current: 当前调度间隔的上边界预测功率 (W)
        :param P_fl_next: 下一个调度间隔的下边界预测功率 (W)
        :param P_fu_next: 下一个调度间隔的上边界预测功率 (W)
        :return: 调度功率 (P_d) 和状态 (status)
        """
        if self.previous_status is None:
            self.previous_status = 'charge'  # 初始化为充电状态

        # 当前调度间隔的功率初始值
        if self.previous_status == 'charge':
            P_d_i = P_fl_current
            P_d_i_minus_1 = self.get_previous_power()
        else:
            P_d_i = P_fu_current
            P_d_i_minus_1 = self.get_previous_power()

        # 计算当前 SOC
        def calculate_soc(P_d_i, P_d_i_minus_1):
            return self.SOC + (P_d_i_minus_1 - P_d_i) * (self.Td / 60) / self.Eb_cap

        SOC_current = calculate_soc(P_d_i, P_d_i_minus_1)
        SOC_next = calculate_soc(P_d_i, P_d_i)

        # 检查 SOC 是否在允许范围内
        status = None
        P_d_adjusted = P_d_i

        if self.previous_status == 'charge':
            # 检查当前 SOC 是否在允许范围内
            if self.SOCl <= SOC_current <= self.SOCu:
                # 检查下一个 SOC 是否在允许范围内
                if self.SOCl <= SOC_next <= self.SOCu:
                    status = 'charge'
                else:
                    P_d_adjusted = self.adjust_power_charge(P_fl_current, SOC_current, SOC_next)
                    status = 'charge'
            else:
                # 调整功率
                P_d_adjusted = self.adjust_power_charge(P_fl_current, SOC_current, SOC_next)
                status = 'charge'
        else:
            # 检查当前 SOC 是否在允许范围内
            if self.SOCl <= SOC_current <= self.SOCu:
                # 检查下一个 SOC 是否在允许范围内
                if self.SOCl <= SOC_next <= self.SOCu:
                    status = 'discharge'
                else:
                    P_d_adjusted = self.adjust_power_discharge(P_fu_current, SOC_current, SOC_next)
                    status = 'discharge'
            else:
                # 调整功率
                P_d_adjusted = self.adjust_power_discharge(P_fu_current, SOC_current, SOC_next)
                status = 'discharge'

        # 更新 SOC 和状态
        self.SOC = calculate_soc(P_d_adjusted, P_d_i_minus_1)
        self.previous_status = status

        return P_d_adjusted, status

    def adjust_power_charge(self, P_fl_current, SOC_current, SOC_next):
        """
        调整充电功率
        """
        # 简单的调整逻辑：如果 SOC 超出上限，降低功率至 P_fl_current * 0.5
        if SOC_next > self.SOCu:
            return P_fl_current * 0.5
        else:
            return P_fl_current

    def adjust_power_discharge(self, P_fu_current, SOC_current, SOC_next):
        """
        调整放电功率
        """
        # 简单的调整逻辑：如果 SOC 低于下限，提高功率至 P_fu_current * 1.5
        if SOC_next < self.SOCl:
            return P_fu_current * 1.5
        else:
            return P_fu_current

    def get_previous_power(self):
        # 实际应用中，需要从存储中获取前一个调度间隔的功率
        return 0  # 假设初始功率为 0


# 示例使用
if __name__ == "__main__":
    # 初始化 BESS 参数
    Eb_cap = 1000  # BESS 能量容量 (Wh)
    Td = 15        # 调度间隔时间 (min)
    SOCl = 0.1     # BESS 的 SOC 下限
    SOCu = 0.9     # BESS 的 SOC 上限

    bess = BatteryEnergyStorageSystem(Eb_cap, Td, SOCl, SOCu)

    # 输入功率预测值
    P_fl_current = 200  # 当前调度间隔下边界预测功率 (W)
    P_fu_current = 400  # 当前调度间隔上边界预测功率 (W)
    P_fl_next = 150     # 下一个调度间隔下边界预测功率 (W)
    P_fu_next = 350     # 下一个调度间隔上边界预测功率 (W)

    # 调用在线功率调度方法
    P_d, status = bess.online_power_dispatching(P_fl_current, P_fu_current, P_fl_next, P_fu_next)

    print(f"调度功率 P_d: {P_d} W")
    print(f"当前 SOC: {bess.SOC}")
    print(f"电池状态: {status}")